package com.example.myapplication.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.airbnb.lottie.LottieAnimationView;
import com.airbnb.lottie.LottieDrawable;
import com.example.myapplication.model.Fruit;
import com.example.myapplication.MyAdapter;
import com.example.myapplication.R;

import java.util.ArrayList;
import java.util.List;

public class AnimationFragment extends Fragment
    implements View.OnClickListener {

    private boolean isPlaying;
    private List<Fruit> dataList;
    private MyAdapter adapter;
    private View mRootView;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        mRootView = inflater.inflate(R.layout.fragment_animation, container, false);
        //找到视图中的 ListView
        ListView lv = mRootView.findViewById(R.id.list_view);

        //创建一个数据容器
        dataList = new ArrayList<>();

        //添加水果
        dataList.add(  new Fruit(R.drawable.a, "苹果",1.1f));
        dataList.add(  new Fruit(R.drawable.b, "草莓",1.2f));
        dataList.add(  new Fruit(R.drawable.c, "樱桃",1.3f));
        dataList.add(  new Fruit(R.drawable.d, "桔子",1.4f));

        //将数据设置到 Adapter 准备适配给 ListView
        adapter = new MyAdapter(dataList, getActivity());

        //把 Adapter 设置给 ListView 将数据显示出来
        lv.setAdapter(adapter);

        final LottieAnimationView animationView1 = mRootView.findViewById(R.id.animation1);
        LottieAnimationView animationView2 = mRootView.findViewById(R.id.animation2);
        animationView1.setAnimation(R.raw.navidad);
        animationView1.playAnimation();
        isPlaying = true;
        animationView1.setRepeatCount(LottieDrawable.INFINITE);
        animationView1.setRepeatMode(LottieDrawable.RESTART);

        animationView2.setAnimation(R.raw.adrock);
        animationView2.playAnimation();
        animationView2.setRepeatCount(LottieDrawable.INFINITE);
        animationView2.setRepeatMode(LottieDrawable.RESTART);


        animationView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isPlaying) {
                    animationView1.pauseAnimation();
                } else {
                    animationView1.resumeAnimation();
                }
                isPlaying = !isPlaying;
            }
        });


        Button add = mRootView.findViewById(R.id.btn_add);
        Button del = mRootView.findViewById(R.id.btn_del);
        add.setOnClickListener(this);
        del.setOnClickListener(this);

        return mRootView;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_add:
                dataList.add(new Fruit(R.drawable.d, "桔子", 1.4f));
                adapter.notifyDataSetChanged();
                break;
            case R.id.btn_del:
                if (dataList.size() > 0) {
                    dataList.remove(dataList.size() - 1);
                }
                adapter.notifyDataSetChanged();
                break;
        }
    }
}
